import { _decorator, Component, Node, CCInteger, Prefab, CCFloat, Sprite, UIOpacity, view } from 'cc';
import RowFallClear_ResurrectionPanel from '../UI/RowFallClear_ResurrectionPanel';
import RowFallClear_GamePlayPanel from '../UI/RowFallClear_GamePlayPanel';
import RowFallClear_Cell from '../Core/RowFallClear_Cell';
import RowFallClear_CellFunctions from '../Core/RowFallClear_CellFunctions';
import RowFallClear_Grid from '../Core/RowFallClear_Grid';
import { RowFallClear_GameData } from '../Data/RowFallClear_GameData';

const { ccclass, property } = _decorator;

export enum GameState {
    Free,
    Fall,
    Match,
    Bomb,
    Pause, //游戏暂停状态
    WinLose, //胜利失败状态
}
export enum GameType {
    Normal, //普通
    Speed, //竞速
}

@ccclass('RowFallClear_GameManager')
export default class RowFallClear_GameManager extends Component {
    public static Instance: RowFallClear_GameManager;

    @property(Node)
    Node_2D: Node | null = null;
    @property(Node)
    Root_grid: Node | null = null;
    @property(Node)
    Root_card: Node | null = null;

    // id: number = 100;

    @property
    add_row: number = 2;
    @property(CCInteger)
    type_count: number = 5;

    cellList: Array<Array<RowFallClear_Cell>> = new Array<Array<RowFallClear_Cell>>();
    movingCellList: Array<RowFallClear_Cell> = new Array<RowFallClear_Cell>();
    killCellList: Array<Array<RowFallClear_Cell>> = new Array<Array<RowFallClear_Cell>>();

    @property(Prefab)
    Prefab_Cell: Prefab | null = null;
    @property(Prefab)
    Prefab_Grid: Prefab | null = null;
    @property(CCFloat)
    padding_horizontal: number = 100;
    @property(CCFloat)
    padding_bottom: number = 140;

    // keyCode: number = -1;
    // width: number;
    // height_screen: number;

    // cellSize: number;
    gameState: GameState;
    gameType: GameType;
    score: number = 0;
    @property(CCInteger)
    timerCount: number = 1;
    @property(CCFloat)
    timer: number = 0;
    isKill: boolean = false;
    userInfo: any;

    onLoad() {
        RowFallClear_GameManager.Instance = this;
    }
    start() {
        // const winSize = view.getVisibleSize();
        // this.width = winSize.width - this.padding_horizontal; //26
        // this.height_screen = winSize.height;
        // this.cellSize = this.width / this.col;

        this.普通模式();
    }

    普通模式() {
        RowFallClear_GameManager.Instance.gameType = GameType.Normal;
        RowFallClear_GameManager.Instance.GameStart();
    }
    竞速模式() {
        RowFallClear_GameManager.Instance.gameType = GameType.Speed;
        RowFallClear_GameManager.Instance.GameStart();
    }

    update(dt) {
        if (RowFallClear_GameManager.Instance.gameType == GameType.Speed && RowFallClear_GameManager.Instance.gameState == GameState.Free) {
            this.timer += dt;
            if (this.timer >= this.timerCount) {
                this.timer = 0;

                let row = RowFallClear_CellFunctions.GetCurrentMovingRow();
                let currentMovingY = row - 1;

                if (currentMovingY - RowFallClear_CellFunctions.GetMaxY() >= 2) {
                    this.timer = 0;

                    for (let x = 0; x <= RowFallClear_GameData.col - 1; x++) {
                        RowFallClear_GameManager.Instance.cellList[x][currentMovingY - 1] = RowFallClear_GameManager.Instance.cellList[x][currentMovingY];
                        let cell: RowFallClear_Cell = RowFallClear_GameManager.Instance.cellList[x][currentMovingY - 1];
                        cell.y -= 1;
                        cell.Refresh();

                        RowFallClear_GameManager.Instance.cellList[x][currentMovingY] = null;
                    }

                    let row2 = RowFallClear_CellFunctions.GetCurrentMovingRow();
                    let currentMovingY2 = row2 - 1;
                    if (currentMovingY2 - RowFallClear_CellFunctions.GetMaxY() < 2) {
                        RowFallClear_GameManager.Instance.scheduleOnce(function () {
                            RowFallClear_CellFunctions.MoveDownAllCell();
                        }, 1);
                    }
                }

            }
        }

        // if (GameManager.Instance.gameState == GameState.Free) {

        // }
        // else if (GameManager.Instance.gameState == GameState.Fall) {

        // }
        // else if (GameManager.Instance.gameState == GameState.Match) {

        // }
        // else if (GameManager.Instance.gameState == GameState.Bomb) {

        // }
    }
    public ChangeStateMachine(_state: GameState) {
        RowFallClear_GameManager.Instance.gameState = _state;

        if (RowFallClear_GameManager.Instance.gameState == GameState.Free) {

        }
        else if (RowFallClear_GameManager.Instance.gameState == GameState.Fall) {
            RowFallClear_CellFunctions.MoveDownAllCell();
        }
        else if (RowFallClear_GameManager.Instance.gameState == GameState.Match) {
            RowFallClear_CellFunctions.KillAllSameList();
        }
        else if (RowFallClear_GameManager.Instance.gameState == GameState.Bomb) {
            RowFallClear_CellFunctions.Bomb();
        }
    }
    public Clear() {
        RowFallClear_CellFunctions.ClearAll();
    }
    public GameStart(_score: number = 0) {
        this.Clear();

        this.timer = 0;
        RowFallClear_GameManager.Instance.isKill = false;
        RowFallClear_GameManager.Instance.gameState = GameState.Free;
        RowFallClear_GameManager.Instance.score = _score;
        RowFallClear_GamePlayPanel.Instance.RefreshScore(RowFallClear_GameManager.Instance.score);

        for (var x = 0; x <= RowFallClear_GameData.col - 1; x++) {
            this.cellList[x] = new Array<RowFallClear_Cell>();
            for (var y = 0; y <= RowFallClear_GameData.row - 1 + this.add_row; y++) {
                var grid: RowFallClear_Grid = RowFallClear_CellFunctions.CreateGrid(0, x, y);
                var sprite = grid.getComponentInChildren(Sprite);
                // if (x % 2 == 0)
                // {
                //     sprite.spriteFrame = SpriteManager.Instance.spriteFrameLists.bgFrameList[0];
                // }
                // else
                // {
                //     sprite.spriteFrame = SpriteManager.Instance.spriteFrameLists.bgFrameList[1];
                // }
                if (y > RowFallClear_GameData.row - 1) {
                    sprite.node.getComponent(UIOpacity).opacity = 150;
                }
            }
        }

        RowFallClear_CellFunctions.CreateTopLine(RowFallClear_GameData.row + 1);
        RowFallClear_CellFunctions.CreateTopLine(RowFallClear_GameData.row + 2);

        RowFallClear_GamePlayPanel.Instance.ShowOn();
    }
    //    // 游戏结束
    public GameOver() {
        this.Clear();
        RowFallClear_GamePlayPanel.Instance.ShowOff();
    }
    //    // 复活
    public Resurrection() {
        RowFallClear_GameManager.Instance.GameStart(RowFallClear_GameManager.Instance.score);
    }
    //    //一回合消除部分结束
    public RoundEnd() {
        RowFallClear_GameManager.Instance.scheduleOnce(function () {
            this.StartNewRound();
        }, 0.05);
    }

    //    //开始新的回合
    public StartNewRound() {
        console.log(`开始新的回合-----`);

        RowFallClear_GameData.IsCanOperate = true;

        RowFallClear_CellFunctions.SetAllNotNew();

        if (RowFallClear_CellFunctions.GetMaxY() >= RowFallClear_GameData.row - 1) {
            // TipPanel.Instance.Show("游戏失败");
            RowFallClear_GameManager.Instance.gameState = GameState.WinLose;
            RowFallClear_ResurrectionPanel.Instance.ShowOn();
        }
        else {
            for (let x = 0; x <= RowFallClear_GameData.col - 1; x++) {
                RowFallClear_GameManager.Instance.cellList[x][RowFallClear_GameData.row + 1 - 1] = RowFallClear_GameManager.Instance.cellList[x][RowFallClear_GameData.row + 2 - 1];
                let cell: RowFallClear_Cell = RowFallClear_GameManager.Instance.cellList[x][RowFallClear_GameData.row + 1 - 1];
                cell.y -= 1;
                cell.initPosition();
                // cell.RefreshPos();
            }

            RowFallClear_CellFunctions.CreateTopLine(RowFallClear_GameData.row + 2);

            RowFallClear_GameManager.Instance.gameState = GameState.Free;
            RowFallClear_GameManager.Instance.isKill = false;
        }
    }
}